var info = "";
var ratio = "";
var upload_button = "";
var options = {
    'bucket': 'motorcycle',
    'save-key': '/Image/{filemd5}{.suffix}',
    'expiration': parseInt((new Date().getTime() + 3600000) / 1000)
};
// 上传请求的相关参数
var policy = window.btoa(JSON.stringify(options));
// 从 UPYUN 用户管理后台获取表单 API
var form_api_secret = '0jWegIsJu9xVxJMrgp4ydONAxok=';
// 计算签名
var signature = md5(policy + '&' + form_api_secret);

var uploader = new plupload.Uploader({
    runtimes: 'html5,flash,silverlight,html4',
    browse_button: 'pickfiles', // you can pass in id...
    container: document.getElementById('container'), // ... or DOM Element itself
    url: 'http://v0.api.upyun.com/' + options.bucket,
    filters : [
        {title : "Image files", extensions : "jpg,gif,png,jpeg"}
    ],
    multipart_params: {
        'Filename': '${filename}', // adding this to keep consistency across the runtimes
        'Content-Type': '',
        'policy': policy,
        'signature': signature
    },

    init: {
        PostInit: function () {
            //确定上传
            document.getElementById('pickfiles-img-right').onclick = function () {
                upload_button = $(this);
                uploader.start();
                return false;
            };

            //取消上传
            document.getElementById('cancel_img').onclick = function () {
                uploader.stop();
                $('.upload-layer').hide();
                return false;
            };
        },

        //本地上传
        FilesAdded: function (up, files) {
                plupload.each(files, function (file) {
                    info += '<div id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b></div>'
                    $('#filelist').html(info)
                });
                for (var i = 0; i < files.length; i++) {
                    if (files && files[i]) {
                        if ($('#QRcode_0').find('img').length >= 9) {
                            $('.loading').hide();
                            layer.msg("最多只能上传9张图片!")
                        } else {
                            if ($('#QRcode_0').find('img').attr('src') == "../../image/base/none_img.png") {
                                $('#QRcode_0').children('div').html('');
                            }
                            var reader = new FileReader();
                            reader.readAsDataURL(files[i].getNative());
                            reader.onload = function (e) {
                                var image = new Image();
                                image.src = e.target.result;
                                image.onload = function () {
                                    var img_num = $('#QRcode_0').find('img').length;
                                    $('#QRcode_0').children('div').append('<img src="' + image.src + ' " style="width:55px;height:50px;border: 1px solid black;float: left;" id="QRcode_kids_0' + img_num + '" class="QRcode_kids_0' + img_num + '">');
                                };
                            }
                        }
                    } else {
                        $('.loading').hide();
                    }
                }
        },

        //上传进度条
        UploadProgress: function (up, file) {
            $('.upload-layer').show();
            $('.upload-layer .percentage em').html(file.percent);
            $(".upload-layer").find('.progressing').width(file.percent.toFixed(1)+"%");
            if(file.percent==100){
                $('.upload-layer').hide();
            }
        },

        //上传成功
        FileUploaded: function (up, file, info) {
            var url = info.response;
            var response = JSON.parse(info.response);
            for (var key in response) {
                if(key === 'url') {
                    img_url= "http://motorcycle.b0.upaiyun.com" + response[key];
                }else if(key === 'image-width') {
                    _img_width= response[key];
                } else if(key === 'image-height') {
                    _img_height= response[key];
                }

            }
            ratio = _img_width / _img_height;
            if (_img_width > 728) {
                addImgHtml_show_height = 728 / ratio
            } else {
                addImgHtml_show_height = _img_height
            }
            num = $('.thread-content-list li').length + 1;
            type = "image";


            addImgHtml();
            addImgThumb();
            //缩略图区域显示/隐藏
            thumbShow();

            upload_button.parents('.add-img-box').slideUp(200);
            $('.add-model').each(function () {
                if ($(this).children('a').length != 0) {
                    //说明不是在页面上原有的按钮,在进行点击,需要将其隐藏
                    $(this).css("height", "0px");
                    $(this).parents("li").find(".insert-model-btn").css({
                        "height": "40px",
                        "border-width": "1px"
                    });
                }
            });
            $('#QRcode_0 div').html('<img src="../../image/base/none_img.png" style="width: 170px;height: 150px;" id="QRcode_kids_0">')
            $('#filelist').html('');
        },

        //上传失败
        Error: function (up, err) {
            //错误信息
            layer.msg("上传出错啦~休息一会儿再试试吧~")
            //document.getElementById('console').appendChild(document.createTextNode("\nError #" + err.code + ": " + err.message));
        }
    }
});

uploader.init();




